{% extends 'base.html' %}
{% block title %}
我的报价单 - 数据管理系统
{% endblock %}
{% block breadcrumb %}
<li class="breadcrumb-item">
  <a href="{{ url_for('list_main') }}">产品列表</a>
</li>
<li class="breadcrumb-item active" aria-current="page">我的报价单</li>
{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
  <h2 class="page-header">我的报价单</h2>
  <a href="{{ url_for('cart.cart') }}" class="btn btn-primary"><i class="bi bi-cart-plus"></i> 去购物车生成报价单</a>
</div>

{% if quotations %}
<div class="card">
  <div class="table-responsive">
    <table class="table table-striped table-hover mb-0">
      <thead class="table-light">
        <tr>
          <th scope="col">报价单编号</th>
          <th scope="col">状态</th>
          <th scope="col">创建时间</th>
          <th scope="col">查看</th>
          {% if current_user.has_permission('admin') %}
          <th scope="col">操作</th>
          {% endif %}
        </tr>
      </thead>
      <tbody>
        {% for quotation in quotations %}
        <tr>
          <td>
            <a href="{{ url_for('cart.view_quotation', quotation_id=quotation.id) }}" class="text-decoration-none">{{
              quotation.quotation_number }}</a>
          </td>
          <td>
            {% if quotation.status == 'draft' %}
            <span class="badge bg-warning">草稿</span>
            {% elif quotation.status == 'submitted' %}
            <span class="badge bg-info">已提交</span>
            {% elif quotation.status == 'approved' %}
            <span class="badge bg-success">已批准</span>
            {% else %}
            <span class="badge bg-secondary">{{ quotation.status }}</span>
            {% endif %}
          </td>
          <td>{{ quotation.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
          <td>
            <a href="{{ url_for('cart.view_quotation', quotation_id=quotation.id) }}"
              class="btn btn-sm btn-outline-primary" target="_blank">查看详情</a>
            <a href="{{ url_for('cart.view_quotation_images_wall', quotation_id=quotation.id) }}"
              class="btn btn-sm btn-outline-primary" target="_blank">查看图片墙</a>
          </td>
          {% if current_user.has_permission('admin') %}
          <td>
            <button class="btn btn-sm btn-outline-danger" onclick="confirmDelete('{{ quotation.id }}')">删除</button>
          </td>
          {% endif %}
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>
{% else %}
<div class="alert alert-info">
  <i class="bi bi-info-circle"></i> 您暂无报价单，快去购物车生成吧！
</div>
{% endif %}
{% endblock %}

{% block scripts %}
<script>
  function confirmDelete(quotationId) {
    if (confirm('确定要删除这个报价单吗？此操作不可撤销！')) {
      fetch(`/quotations/${quotationId}`, {
        method: 'DELETE',
        headers: {
          'Content-Type': 'application/json',
        }
      })
        .then(response => {
          if (response.ok) {
            window.location.reload();
          } else {
            alert('删除失败，请重试');
          }
        })
        .catch(error => {
          console.error('Error:', error);
          alert('删除过程中发生错误');
        });
    }
  }



</script>
{% endblock %}